/*
 * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.fbreader.library;


import com.example.administrator.fangbook.appliction.HtlApplication;

import org.geometerplus.android.fbreader.SQLiteBooksDatabase;
import org.geometerplus.fbreader.book.Bookmark;
import org.geometerplus.fbreader.book.FileInfo;
import org.geometerplus.fbreader.book.FileInfoSet;
import org.geometerplus.fbreader.book.M17kPlainTxtBook;
import org.geometerplus.fbreader.book.MTxtBook;
import org.geometerplus.fbreader.book.ReadBook;
import org.geometerplus.fbreader.book.SeriesInfo;
import org.geometerplus.fbreader.book.Tag;
import org.geometerplus.fbreader.filetype.FileType17KTxtNovel;
import org.geometerplus.fbreader.filetype.FileTypeTxt;
import org.geometerplus.zlibrary.core.filesystem.ZL17KPlainTxtFile;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filesystem.ZLTxtFile;
import org.geometerplus.zlibrary.text.view.ZLTextPosition;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

public abstract class BooksDatabase {
	private static BooksDatabase ourInstance;

	public synchronized static BooksDatabase Instance() {
		if(ourInstance == null)
		{
			ourInstance = new SQLiteBooksDatabase(HtlApplication.getApplication(), "READER");
		}
		return ourInstance;
	}

	public BooksDatabase() {
		ourInstance = this;
	}


	public ReadBook createBook(long id, long fileId, String title, String encoding, String language, String fileMode) {
		final FileInfoSet infos = new FileInfoSet(this, fileId);
		return createBook(id, infos.getFile(fileId), title, encoding, language, fileMode);
	}
	public ReadBook createBook(long id, ZLFile file, String title, String encoding, String language, String fileMode) {
		if(fileMode != null && fileMode.startsWith(FileType17KTxtNovel.FileMode) && file instanceof ZL17KPlainTxtFile)
		{
			return new M17kPlainTxtBook(id, (ZL17KPlainTxtFile)file, ((ZL17KPlainTxtFile)file).getBookName(), encoding, language);
		}
		else if(fileMode != null && fileMode.startsWith(FileTypeTxt.FileMode) && file instanceof ZLTxtFile)
		{
			return new MTxtBook(id, (ZLTxtFile)file, ((ZLTxtFile)file).getBookName(), encoding, language);
		}
		return (file != null) ? new ReadBook(id, file, title, encoding, language) : null;
	}
	public void addAuthor(ReadBook book, Author author) {
		book.addAuthor(author);
	}
	public void addTag(ReadBook book, Tag tag) {
		book.addTag(tag);
	}
	public void setSeriesInfo(ReadBook book, String series, float index) {
		book.setSeriesInfo(series, new BigDecimal(index));
	}

	public abstract void executeAsATransaction(Runnable actions);

	// returns map fileId -> book
	public abstract Map<Long,ReadBook> loadBooks(FileInfoSet infos, boolean existing);
	public abstract void setExistingFlag(Collection<ReadBook> books, boolean flag);
	public abstract ReadBook loadBook(long bookId);
	public abstract void reloadBook(ReadBook book);
	public abstract ReadBook loadBookByFile(long fileId, ZLFile file);

	public abstract List<Author> loadAuthors(long bookId);
	public abstract List<Tag> loadTags(long bookId);
	public abstract SeriesInfo loadSeriesInfo(long bookId);
	public abstract void updateBookInfo(long bookId, long fileId, String encoding, String language, String title);
	public abstract long insertBookInfo(ZLFile file, String encoding, String language, String title, String file_mode);
	public abstract void deleteAllBookAuthors(long bookId);
	public abstract void saveBookAuthorInfo(long bookId, long index, Author author);
	public abstract void deleteAllBookTags(long bookId);
	public abstract void saveBookTagInfo(long bookId, Tag tag);
	public abstract void saveBookSeriesInfo(long bookId, SeriesInfo seriesInfo);

	public FileInfo createFileInfo(long id, String name, FileInfo parent) {
		return new FileInfo(name, parent, id);
	}

	public abstract Collection<FileInfo> loadFileInfos();
	public abstract Collection<FileInfo> loadFileInfos(ZLFile file);
	public abstract Collection<FileInfo> loadFileInfos(long fileId);
	public abstract void removeFileInfo(long fileId);
	public abstract void saveFileInfo(FileInfo fileInfo);

	public abstract List<Long> loadRecentBookIds();
	public abstract void saveRecentBookIds(final List<Long> ids);

	public abstract List<Long> loadFavoritesIds();
	public abstract void addToFavorites(long bookId);
	public abstract void removeFromFavorites(long bookId);

	public Bookmark createBookmark(long id, long bookId, String bookTitle, String text, Date creationDate, Date modificationDate, Date accessDate, int accessCounter, String modelId, int paragraphIndex, int wordIndex, int charIndex, boolean isVisible, String tag) {
		return new Bookmark(id, bookId, bookTitle, text, creationDate, modificationDate, accessDate, accessCounter, modelId, paragraphIndex, wordIndex, charIndex, isVisible, tag);
	}

	public abstract List<Bookmark> loadBookmarks(long bookId, boolean isVisible);
	public abstract List<Bookmark> loadBookmarks(long bookId, boolean isVisible, String tag);
	public abstract List<Bookmark> loadAllVisibleBookmarks();
	public abstract long saveBookmark(Bookmark bookmark);
	public abstract void deleteBookmark(Bookmark bookmark);

	public abstract ZLTextPosition getStoredPosition(long bookId);
	public abstract void storePosition(long bookId, ZLTextPosition position);

	public abstract boolean insertIntoBookList(long bookId);
	public abstract boolean deleteFromBookList(long bookId);
	public abstract boolean checkBookList(long bookId);

	public abstract Collection<String> loadVisitedHyperlinks(long bookId);
	public abstract void addVisitedHyperlink(long bookId, String hyperlinkId);

	
}
